 
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
import { MainStrategy } from "GameLoop/WorldState/EntityManager/CreateStrategy/MainStrategy";
import { getRoleConfig } from "utils/constants/bodyconstant";
import { priority } from "utils/constants/definetypes";
import { BodyUtils } from "utils/creep/bodyutils";
import { Roles } from "utils/creep/setups";
import { PositionHelper } from "utils/position/PositionHelper";
import { generateUniqueId } from "utils/utils";

/**
 * 转移spawn的能量到core
 */
export class TransportSpawnDecision< T extends "creep"="creep"> extends ABaseDecision<T>
{

    public isExpired(event: NormalEvent, region: string): boolean 
    {

 
        const lordroom = Game.rooms[event.region] as Required<Room>;
        if(lordroom.controller.level<5)
        {
            return false;
        }
        if(!lordroom.canExecute(51))
        {
            return false;
        }
        const task = lordroom.findTaskById(`${event.room}_transfer`,"creep");
        if(task)
        {
            return false
        }
        return true;
    }
    public makeTask(event: IFillStoreEvent, region: string): IDecisionCode<T>|false
    {
        // global.Log.Info(`维修任务`);
        const lordroom = Game.rooms[event.region] as Required<Room>;
        
        const spawnentity = lordroom.getEntity("SpawnEntity");
        
        const coreEntity = lordroom.getEntity("CoreEntity");
       
        if(  spawnentity.Link && spawnentity.Link.store.getUsedCapacity(RESOURCE_ENERGY)>=100)
        {
            const stores = coreEntity.getStores({resourceType:RESOURCE_ENERGY,amount:50000});
            const takeobject = spawnentity.getWithdrawObjects({resourceType:RESOURCE_ENERGY,amount:1600});// spawn 满了

            if( stores.length>0 && takeobject.length>0)
            {
                const Task:ICreepTaskData<"transport"> = 
                {
                    type:"transport",
                    arg:{
                        From:PositionHelper.getObjectRef(takeobject[0]),
                        To:PositionHelper.getObjectRef(stores[0]),
                        ResourceType:RESOURCE_ENERGY,
                        
                    },
                    creeps:[],
                    room:event.room,
                    body:{},
                    income:-1,
                    cost:50,
                    required:[
                        [['carry'], 1]
                    ],
                    disableSpawn:true
                }
                const code:IDecisionCode<T>={
                    region:event.region,
                    isunique:false,
                    task:[{
                        id:`${event.room}_transfer`,
                        ttl:5000,
                        priority:priority.B,
                        type:"creep",
                        data:Task,
                        shard:1,
                    }],
                }
                return code;
            }
        }

        return false;
    }
    
}